package cn.ming.wfchat.app.me.activity;

import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import cn.ming.wfchat.R;
import cn.ming.wfchat.app.AppService;
import cn.ming.wfchat.app.dialog.PaymentDialog;
import cn.ming.wfchat.app.dynamic.adpter.NetWorkListAdapter;
import cn.ming.wfchat.app.main.ConfigFindBean;
import cn.ming.wfchat.kit.WfcBaseActivity;
import cn.ming.wfchat.kit.contact.ContactViewModel;
import cn.ming.wfchat.kit.contact.model.UIUserInfo;
import cn.ming.wfchat.kit.net.base.StatusResult;
import cn.ming.wfchat.kit.utils.Tools;
import cn.wildfirechat.remote.ChatManager;

/**
 * @author luoming
 * created at 2020/8/11 3:04 PM
 * 加入关系网
 */
public class FindNetWorkActivity extends WfcBaseActivity {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    protected NetWorkListAdapter userListAdapter;
    protected ContactViewModel contactViewModel;
    private String relationAmount;

    @Override
    protected int contentLayout() {
        return R.layout.network_activity;
    }

    @Override
    protected void afterViews() {
        super.afterViews();
        setTitle("加入他人关系网");
        initView();
    }

    void initView() {
        congfigFind();
        contactViewModel = ViewModelProviders.of(this).get(ContactViewModel.class);
        userListAdapter = new NetWorkListAdapter();
        userListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tv_join) {
                    PaymentDialog.newInstance("付款", relationAmount + "USDT").setOnClickListener(new PaymentDialog.OnClickListener() {
                        @Override
                        public void getPwd(String pwd) {
                            Joinetwork(pwd, userListAdapter.getData().get(position).getUserInfo().uid);
                        }
                    }).show(FindNetWorkActivity.this);
                }
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        contactViewModel.contactListLiveData().observe(this, userInfos -> {
            List<UIUserInfo> uiUserInfoList = new ArrayList<>();
            for (UIUserInfo info : userInfos) {
                if (Tools.isInteger(info.getUserInfo().uid)) {
                    uiUserInfoList.add(info);
                }
            }
            userListAdapter.setNewData(uiUserInfoList);
            recyclerView.setAdapter(userListAdapter);
        });
    }

    /**
     * 获取配置信息
     */
    void congfigFind() {
        AppService.Instance().configFind("RELATION_AMOUNT", new AppService.ConfigFindCallback() {
            @Override
            public void onUiSuccess(ConfigFindBean assetsBean) {
                relationAmount = assetsBean.getValue();
            }

            @Override
            public void onUiFailure(int code, String msg) {

            }
        });
    }


    /**
     * 加入关系网
     */
    void Joinetwork(String pwd, String uid) {
        AppService.Instance().addNetWork(pwd, uid, new AppService.DynamicsLikesCallback() {
            @Override
            public void onUiSuccess(StatusResult assetsBean) {
                finish();
            }

            @Override
            public void onUiFailure(int code, String msg) {
                ChatManager.showToast(msg);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        contactViewModel.reloadContact();
        contactViewModel.reloadFriendRequestStatus();
    }
}
